package com.hx.vending.service.impl;

import com.hx.vending.bean.SubsidiaryRole;
import com.hx.vending.bean.UserOperation;
import com.hx.vending.bean.entity.SuperOperation;
import com.hx.vending.bean.entity.UserOperationShow;
import com.hx.vending.dao.SubsidiaryRoleMapper;
import com.hx.vending.dao.UserOperationMapper;
import com.hx.vending.service.IUserOperationService;
import com.hx.vending.util.Msg;
import com.hx.vending.util.ToolUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/3/12.
 */
@Service
public class IUserOperationServiceImpl implements IUserOperationService {

    @Resource
    private UserOperationMapper userOperationMapper;

    @Resource
    private SubsidiaryRoleMapper subsidiaryRoleMapper;


    /**
     * 查询权限列表
     * @return
     */
    @Override
    public Msg selectOperationList() {

        UserOperation userOperation = new UserOperation();
        List<UserOperation> userOperationList = this.userOperationMapper.selectByExampleSelective(userOperation);

        if (ToolUtil.isEmpty(userOperationList)) {
            return Msg.fail();
        }

        //查询完之后将菜单嵌套
        List<Object> Super = new ArrayList<Object>();

        //保存父级
        for (UserOperation uo: userOperationList) {
            if (ToolUtil.isEmpty(uo.getOperationSuperId())) {

                SuperOperation superOperation = new SuperOperation();
                superOperation.setUserOperation(uo);

                List<Object> son = new ArrayList<Object>();
                for (UserOperation sonuo: userOperationList) {
                    if (ToolUtil.isNotEmpty(sonuo.getOperationSuperId())) {
                        if (sonuo.getOperationSuperId() == uo.getOperationId()) {
                            son.add(sonuo);
                        }
                    }
                }

                superOperation.setOperationList(son);

                Super.add(superOperation);
            }
        }

        return Msg.success().add("operationList", Super);
    }

    /**
     * 添加权限
     * @param userOperation
     * @return
     */
    @Override
    public Msg insertUserOperation(UserOperation userOperation) {
        if (ToolUtil.isEmpty(userOperation.getOperationName())) {
            return Msg.fail().add("msg", "权限路由名称为空");
        }
        if (ToolUtil.isEmpty(userOperation.getOperationTitle())) {
            return Msg.fail().add("msg", "权限标题名称为空");
        }

        int i = this.userOperationMapper.insertSelective(userOperation);

        if (i != 0) {
            return Msg.success();
        }else {
            return Msg.fail().add("msg", "添加失败");
        }
    }


    /**
     * 根据角色岗位查询拥有权限
     * @param subsidiaryRole
     * @return
     */
    @Override
    public Msg selectOperationByRole(SubsidiaryRole subsidiaryRole) {
        if (ToolUtil.isEmpty(subsidiaryRole.getSubsidiaryRoleId())) {
            return Msg.fail().add("msg", "岗位id为空");
        }

        //保存权限信息
        List<UserOperation> userOperations = new ArrayList<UserOperation>();
        //查询完之后将菜单嵌套
        List<Object> Super = new ArrayList<Object>();

        //查询岗位信息
        SubsidiaryRole selectSubsidiaryRole = this.subsidiaryRoleMapper.selectByPrimaryKeySelective(subsidiaryRole);

        if (ToolUtil.isNotEmpty(selectSubsidiaryRole)) {
            if (ToolUtil.isNotEmpty(selectSubsidiaryRole.getSubsidiaryRoleOperation())) {
                String[] operationId = selectSubsidiaryRole.getSubsidiaryRoleOperation().split(",");
                for (String opid: operationId) {
                    //查询权限信息
                    UserOperation userOperation = new UserOperation();
                    userOperation.setOperationId(Integer.parseInt(opid));
                    UserOperation selectUserOperation = this.userOperationMapper.selectByPrimaryKeySelective(userOperation);

                    userOperations.add(selectUserOperation);
                }
                //保存父级
                for (UserOperation uo: userOperations) {
                    if (ToolUtil.isEmpty(uo.getOperationSuperId())) {

//                        UserOperationShow superOperation = new UserOperationShow();
                        SuperOperation superOperation = new SuperOperation();
//                        superOperation.setOperationId(uo.getOperationId());
//                        superOperation.setOperationCreateTime(uo.getOperationCreateTime());
//                        superOperation.setOperationName(uo.getOperationName());
//                        superOperation.setOperationPath(uo.getOperationPath());
//                        superOperation.setOperationRemark(uo.getOperationRemark());
//                        superOperation.setOperationSuperId(uo.getOperationSuperId());
//                        superOperation.setOperationTitle(uo.getOperationTitle());
                        superOperation.setUserOperation(uo);

                        List<Object> son = new ArrayList<Object>();
                        for (UserOperation sonuo: userOperations) {
                            if (ToolUtil.isNotEmpty(sonuo.getOperationSuperId())) {
                                if (sonuo.getOperationSuperId() == uo.getOperationId()) {
                                    son.add(sonuo);
                                }
                            }
                        }

                        superOperation.setOperationList(son);

                        Super.add(superOperation);
                    }
                }
                return Msg.success().add("allOperationList", Super);
            }else {
                return Msg.success().add("allOperationList", Super);
            }
        }else {
            return Msg.fail().add("msg", "该角色不存在");
        }
    }


}
